package edu.uwm.universitydb.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.uwm.universitydb.model.Role;

public class Main {
	
	public void build() {
		_frame = new JFrame("University DB");
		_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		_frame.setIconImage(new ImageIcon("res/icon.png").getImage());
				
		_frame.add(this.buildHeader(), BorderLayout.NORTH);
		_frame.add(this.buildAppPanel(), BorderLayout.CENTER);
				
		
	}
	
	public void show() {
		_frame.pack();
		_frame.setBounds(150, 50, 1024, 600);
		_frame.setVisible(true);
	}
	
	private JPanel buildHeader() {
		JPanel header = new JPanel();
		header.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1.0;		
		
		
		c.gridx = 0;
		c.gridy = 0;
		header.add(this.buildTopBorder(), c);
		
		c.gridy = 1;
		header.add(this.buildLogoPanel(), c);
		
		c.gridy = 2;
		header.add(this.buildMenuBar(), c);
		
		return header;
	}
	
	private JPanel buildAppPanel() {
		_cards = new JPanel();
		_cards.setLayout(new CardLayout());
		
		return _cards;
	}
	
	private JPanel buildTopBorder() {
		JPanel topBorder = new JPanel();
		topBorder.setPreferredSize(new Dimension(100, 7));
		topBorder.setBackground(Color.LIGHT_GRAY);
		topBorder.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.GRAY));
		return topBorder;
	}
	
	private JPanel buildLogoPanel() {
		JLabel imgLabel = new JLabel();
		try {
			BufferedImage img = ImageIO.read(new File("res/logo-uwm.gif"));
			imgLabel = new JLabel(new ImageIcon(img));
		}
		catch (IOException ex) {
			System.out.println("ERROR: Can't load image:\n" + ex.getMessage());
		}
		_logout = new JLabel("Logout");
		_logout.setName(Main.LOGOUT);
		_logout.setCursor(new Cursor(Cursor.HAND_CURSOR));
		
		JPanel logo = new JPanel();
		logo.setLayout(new BorderLayout());
		//logo.setPreferredSize(new Dimension(100, 50));
		logo.setBackground(Color.WHITE);
		logo.setBorder(BorderFactory.createMatteBorder(5, 10, 5, 40, Color.WHITE));
		logo.add(imgLabel, BorderLayout.WEST);
		logo.add(_logout, BorderLayout.EAST);
		return logo;
	}
	
	private JPanel buildMenuBar() {
		_menu = new JPanel();
		_menu.setPreferredSize(new Dimension(100, 30));
		_menu.setLayout(new FlowLayout(FlowLayout.LEFT, 16, 5));
		_menu.setBackground(new Color(255, 199, 11));
		_menu.setBorder(BorderFactory.createMatteBorder(1, 0, 4, 0, new Color(255, 168, 0)));
		//this.buildMenuItems(menu);
		return _menu;
	}
	
	public void buildMenuItems(Role role) {
		_studentMntMenu = new JLabel("Students");
		_studentMntMenu.setName(Main.STUDENTSMENU);
		_studentMntMenu.setCursor(new Cursor(Cursor.HAND_CURSOR));
		_menu.add(_studentMntMenu);
		
		_projectMntMenu = new JLabel("Projects");
		_projectMntMenu.setName(Main.PROJECTMENU);
		_projectMntMenu.setCursor(new Cursor(Cursor.HAND_CURSOR));
		_menu.add(_projectMntMenu);
		
		if (role == Role.ADMINISTRATOR) {
			_professorMntMenu = new JLabel("Professors");
			_professorMntMenu.setName(Main.PROFESSORSMENU);
			_professorMntMenu.setCursor(new Cursor(Cursor.HAND_CURSOR));
			_menu.add(_professorMntMenu);
			_userMntMenu = new JLabel("Users");
			_userMntMenu.setName(Main.USERSMENU);
			_userMntMenu.setCursor(new Cursor(Cursor.HAND_CURSOR));
			_menu.add(_userMntMenu);
			_departmentMntMenu = new JLabel("Departments");
			_departmentMntMenu.setName(Main.DEPARTMENTMENU);
			_departmentMntMenu.setCursor(new Cursor(Cursor.HAND_CURSOR));
			_menu.add(_departmentMntMenu);
			_performanceMntMenu = new JLabel("Performance Testing");
			_performanceMntMenu.setName(Main.PERFORMANCEMENU);
			_performanceMntMenu.setCursor(new Cursor(Cursor.HAND_CURSOR));
			_menu.add(_performanceMntMenu);
		}
	}
	
	public void clearMenuItems() {
		_menu.removeAll();
		_menu.revalidate();
		_menu.repaint();
	}
	
	public JLabel getLogoutLink() {
		return _logout;
	}
	
	public JLabel getStudentsMenu() {
		return _studentMntMenu;
	}
	
	public JLabel getProfessorsMenu() {
		return _professorMntMenu;
	}
	
	public JLabel getUsersMenu() {
		return _userMntMenu;
	}
	
	public JLabel getProjectsMenu() {
		return _projectMntMenu;
	}
	
	public JLabel getDepartmentMenu() {
		return _departmentMntMenu;
	}
	
	public JLabel getPerformanceMenu() {
		return _performanceMntMenu;
	}
	public JFrame getFrame() {
		return _frame;
	}
	public void addCard(JPanel card, String cardId) {
		_cards.add(card, cardId);
	}
	
	public void setCard(String cardId) {
		CardLayout layout = (CardLayout)(_cards.getLayout());
		layout.show(_cards, cardId);
	}
	
	private JFrame _frame;
	private JPanel _cards;
	private JLabel _logout;
	//private JLabel _loginMenu;
	private JPanel _menu;
	private JLabel _studentMntMenu;
	private JLabel _professorMntMenu;
	private JLabel _userMntMenu;
	private JLabel _projectMntMenu;
	private JLabel _departmentMntMenu;
	private JLabel _performanceMntMenu;

	public static final String LOGOUT = "logout";
	public static final String STUDENTSMENU = "StudentMntMenu";
	public static final String PROFESSORSMENU = "ProfessorMntMenu";
	public static final String USERSMENU = "UserMntMenu";
	public static final String PROJECTMENU = "ProjectMntMenu";
	public static final String DEPARTMENTMENU = "DepartmentMntMenu";
	public static final String PERFORMANCEMENU = "PerfMntMenu";

}
